﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;

namespace Homebound
{
    class WinState : State
    {
        SpriteFont font;
        SpriteFont keyFont;
        Texture2D background;
        const float LINE_SEPERATION = 40f;
        Level level;

        // Bonuses
        const int BONUS_PER_HP = 25;
        const int BONUS_PER_LIVE = 150;
        const int BONUS_ALL_GEMS = 100;

        // Scores
        int totalScore;
        int healthBonus;
        int livesBonus;
        int allGemBonus;

        // Bonus for all enemies killed
        int enemyKilledBonus;

        public WinState(GraphicsDeviceManager newGraphics, GameServiceContainer newServices, ContentManager newContent)
            : base(newGraphics, newServices, newContent)
        {
            level = null;
        }

        // Load the game's external content.
        public override void loadContent()
        {
            // Load overlay textures
            font = content.Load<SpriteFont>("Fonts/Choice");
            keyFont = content.Load<SpriteFont>("Fonts/Hud");
            background = content.Load<Texture2D>("nebula_1");
            base.loadContent();
        }

        // Called whenever a new state is selected.
        public override void onEnter(State state)
        {
            loadContent();

            GamePlayState gamePlayState = (GamePlayState)state;

            totalScore = gamePlayState.level.score;
            healthBonus = BONUS_PER_HP * gamePlayState.level.player.life;
            totalScore += healthBonus;
            livesBonus = BONUS_PER_LIVE * gamePlayState.level.player.lives;
            totalScore += livesBonus;

            if (gamePlayState.level.orbC == gamePlayState.level.totalOrbC) // All orbs were collected.
            {
                allGemBonus = BONUS_ALL_GEMS;
                totalScore += allGemBonus;
            }

            // Set enemy killed bonus
            enemyKilledBonus = (level.player.killCount / level.enemyC) * 300;
            totalScore += enemyKilledBonus;

            enterPressed = false;
            base.onEnter(state);
        }

        // Called when the state is changing.
        public override void onExit(State state)
        {
            base.onExit(state);
        }

        // Draw the visual representation of the state.
        public override void draw(SpriteBatch spriteBatch, GameTime gameTime, Vector2 screenSize)
        {
            Vector2 textPos = new Vector2(screenSize.X / 2, 0);

            spriteBatch.Begin();
                spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
                spriteBatch.DrawString(font, "WIN", textPos, Color.White);
                textPos = new Vector2(textPos.X, textPos.Y + (2 * LINE_SEPERATION));

                // Orb Score
                if (level != null)
                {
                    spriteBatch.DrawString(keyFont, "Orb Score: " + level.score, textPos, Color.Red);
                }

                // Lives Bonus
                textPos = new Vector2(textPos.X, textPos.Y + LINE_SEPERATION);
                spriteBatch.DrawString(keyFont, "Lives Bonus: " + livesBonus, textPos, Color.Red);

                // Health Score
                textPos = new Vector2(textPos.X, textPos.Y + LINE_SEPERATION);
                spriteBatch.DrawString(keyFont, "Health Bonus: " + healthBonus, textPos, Color.Red);

                // All Gem Bonus
                textPos = new Vector2(textPos.X, textPos.Y + LINE_SEPERATION);
                spriteBatch.DrawString(keyFont, "All Gem Bonus: " + allGemBonus, textPos, Color.Red);

                // Enemies killed bonus
                textPos = new Vector2(textPos.X, textPos.Y + LINE_SEPERATION);
                spriteBatch.DrawString(keyFont, "All enemies killed: " + enemyKilledBonus, textPos, Color.Red);

                // Total Score
                textPos = new Vector2(textPos.X, textPos.Y + LINE_SEPERATION);
                spriteBatch.DrawString(keyFont, "Total Score: " + totalScore, textPos, Color.Red);
            spriteBatch.End();
            base.draw(spriteBatch, gameTime, screenSize);
        }

        // Handles all input for the current state, (Keyboard, GamePad).
        public override void input(GameTime gameTime, List<Keys> keys, GamePadButtons buttons, GamePadDPad dpad, GamePadThumbSticks thumbSticks)
        {
        }

        // Handles all update operations for the current state, (Keyboard, GamePad).
        public override void update(GameTime gameTime)
        {
            base.update(gameTime);
        }

        // Conditions necessary to enter go return to a next state.
        public override bool goingForward(State prev)
        {
            GamePlayState gamePlayState = (GamePlayState)prev;

            level = gamePlayState.level;
            return (gamePlayState.level.ReachedExit);
        }
    }
}
